package com.ruoyi.web.controller.biz;

import java.util.List;

import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.service.biz.IBmMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.biz.BmAmountDetailRecord;
import com.ruoyi.system.service.biz.IBmAmountDetailRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员消费明细Controller
 * 
 * @author ruoyi
 * @date 2022-11-18
 */
@Controller
@RequestMapping("/biz/detail")
public class BmAmountDetailRecordController extends BaseController
{
    private String prefix = "biz/detail";

    @Autowired
    private IBmAmountDetailRecordService bmAmountDetailRecordService;
    @Autowired
    private IBmMemberService bmMemberService;

    @RequiresPermissions("biz:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询会员消费明细列表
     */
    @RequiresPermissions("biz:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BmAmountDetailRecord bmAmountDetailRecord)
    {
        startPage();
        List<BmAmountDetailRecord> list = bmAmountDetailRecordService.selectBmAmountDetailRecordList(bmAmountDetailRecord);
        return getDataTable(list);
    }

    /**
     * 导出会员消费明细列表
     */
    @RequiresPermissions("biz:record:export")
    @Log(title = "会员消费明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BmAmountDetailRecord bmAmountDetailRecord)
    {
        List<BmAmountDetailRecord> list = bmAmountDetailRecordService.selectBmAmountDetailRecordList(bmAmountDetailRecord);
        ExcelUtil<BmAmountDetailRecord> util = new ExcelUtil<BmAmountDetailRecord>(BmAmountDetailRecord.class);
        return util.exportExcel(list, "会员消费明细数据");
    }

    /**
     * 新增会员消费明细
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("members", bmMemberService.selectAllBmMember());
        return prefix + "/add";
    }

    /**
     * 新增保存会员消费明细
     */
    @RequiresPermissions("biz:record:add")
    @Log(title = "会员消费明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BmAmountDetailRecord bmAmountDetailRecord)
    {
        getSysUser().getDeptId();
        for (int i=0;i<bmAmountDetailRecord.getMembers().length;i++){
            bmAmountDetailRecord.setAccount(bmAmountDetailRecord.getMembers()[i]);
            bmAmountDetailRecordService.insertBmAmountDetailRecord(bmAmountDetailRecord);
        }

        return AjaxResult.success("保存成功！");
    }

    /**
     * 修改会员消费明细
     */
    @RequiresPermissions("biz:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BmAmountDetailRecord bmAmountDetailRecord = bmAmountDetailRecordService.selectBmAmountDetailRecordById(id);
        mmap.put("bmAmountDetailRecord", bmAmountDetailRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存会员消费明细
     */
    @RequiresPermissions("biz:record:edit")
    @Log(title = "会员消费明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BmAmountDetailRecord bmAmountDetailRecord)
    {

        return toAjax(bmAmountDetailRecordService.updateBmAmountDetailRecord(bmAmountDetailRecord));
    }

    /**
     * 删除会员消费明细
     */
    @RequiresPermissions("biz:record:remove")
    @Log(title = "会员消费明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bmAmountDetailRecordService.deleteBmAmountDetailRecordByIds(ids));
    }
}
